"""区块链连接和信息相关路由"""

from flask import Blueprint, request, current_app
from flasgger import swag_from
from utils.response import create_response
from utils.exceptions import BlockchainMiddlewareException

chain_bp = Blueprint('chain', __name__)

@chain_bp.route('/chains', methods=['GET'])
@swag_from({
    'tags': ['Chain'],
    'summary': '获取支持的区块链列表',
    'description': '返回当前支持的所有区块链平台列表',
    'security': [{'ApiKeyAuth': []}],
    'responses': {
        200: {
            'description': '成功返回区块链列表',
            'schema': {
                'type': 'object',
                'properties': {
                    'success': {'type': 'boolean'},
                    'data': {
                        'type': 'array',
                        'items': {'type': 'string'}
                    },
                    'requestId': {'type': 'string'}
                }
            }
        }
    }
})
def get_supported_chains():
    """获取支持的区块链列表"""
    try:
        # 返回支持的区块链类型列表
        chains = ['fisco_bcos', 'chainmaker', 'jdchain', 'xuperchain', 'thanos']
        return create_response(data={'chains': chains})
    except Exception as e:
        return create_response(
            success=False,
            error_code='CHAIN_LIST_ERROR',
            error_message=str(e),
            status_code=500
        )



@chain_bp.route('/chains/<chain_type>/info', methods=['GET'])
@swag_from({
    'tags': ['Chain'],
    'summary': '获取区块链信息',
    'description': '获取指定区块链的基本信息',
    'security': [{'ApiKeyAuth': []}],
    'parameters': [
        {
            'name': 'chain_type',
            'in': 'path',
            'type': 'string',
            'required': True,
            'description': '区块链类型'
        }
    ],
    'responses': {
        200: {
            'description': '成功返回区块链信息',
            'schema': {
                'type': 'object',
                'properties': {
                    'success': {'type': 'boolean'},
                    'data': {
                        'type': 'object',
                        'properties': {
                            'chain_id': {'type': 'string'},
                            'latest_block': {'type': 'integer'},
                            'node_count': {'type': 'integer'},
                            'status': {'type': 'string'}
                        }
                    },
                    'requestId': {'type': 'string'}
                }
            }
        }
    }
})
def get_chain_info(chain_type):
    """获取区块链信息"""
    try:
        # 验证区块链类型
        supported_chains = ['fisco_bcos', 'ethereum', 'chainmaker', 'jdchain', 'xuperchain', 'thanos']
        if chain_type not in supported_chains:
            return create_response(
                success=False,
                error_code='INVALID_CHAIN_TYPE',
                error_message=f'Unsupported chain type: {chain_type}',
                status_code=400
            )
        
        info = current_app.unified_layer.get_chain_info(chain_type)
        return create_response(data=info)
    except Exception as e:
        return create_response(
            success=False,
            error_code='CHAIN_INFO_ERROR',
            error_message=str(e),
            status_code=500
        )